package com.fingard.dsp.bank.directbank.jscb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

public class JSCB01Base extends DirectBase implements ITcpClient {
    protected static TransIDHelper idHelper = new TransIDHelper(1, 9999);
    protected String getQryTransID(){
        return Format.DateTime12Year2Format.format(new Date())+idHelper.getNextIDStr();
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        //一：参数、对象定义
        String[] retStrArr = new String[]{"", ""};
        String tmpCharset = getCharset();
        String IP  = bankFront.serverIP;
        String port = bankFront.serverPort;
        WriteBankLogLn("1.提交银行的地址：" + IP+ ":" + port);
        TcpPortClient tmpClient = new TcpPortClient(IP, Integer.parseInt(port), this);

        //二：组织报头
        byte[] tmpBytesBody;
        String tmpHead;
        p_reqStr = "10" + p_reqStr;
        tmpBytesBody =  p_reqStr.getBytes(tmpCharset);
        tmpHead = String.format("%-10d", tmpBytesBody.length);

        byte[] tmpBytesHead = tmpHead.getBytes(tmpCharset);

        //三：封装请求报文：【报头】、【报体】
        byte[] tmpBytesSend = new byte[tmpBytesHead.length + tmpBytesBody.length];
        System.arraycopy(tmpBytesHead, 0, tmpBytesSend, 0, tmpBytesHead.length);
        System.arraycopy(tmpBytesBody, 0, tmpBytesSend, tmpBytesHead.length, tmpBytesBody.length);

        //四：发送报文
        WriteBankLogStep2(tmpHead + p_reqStr);
        TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
        //五：解析报文
        if (StringHelper.isNullOrEmpty(tmpReceiver.connState)) {
            String tmpBkStr = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, tmpCharset);
            WriteBankLogStep3(tmpBkStr);
        }
        retStrArr[0] = tmpReceiver.connState;
        if (tmpReceiver.isCompleted) {
            retStrArr[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex, tmpReceiver.contentLength, tmpCharset);
        } else {
            retStrArr[1] = tmpReceiver.getConnMsg();
            WriteBankLogLn(retStrArr[1]);
        }

        return retStrArr;
    }

    protected String getCharset() {
        return "GBK";
    }

    @Override
    public void getContentLength(TcpReceiver tcpReceiver) throws Exception {
        if (tcpReceiver.contentLength == -1) {
            if (tcpReceiver.readedLength >= 12) {
                String tmpHead = new String(tcpReceiver.recBytes, 0, 10, getCharset());
                tcpReceiver.contentLength = Integer.parseInt(tmpHead.trim()) - 2;
                tcpReceiver.contentStartIndex = 12;
            }
        }
    }

    public void writeCommonHeader(XmlTextWriter xmlWriter,JSCB01Header header){
        xmlWriter.writeStartDocument();
        xmlWriter.writeStartElement("ap");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("tr_code", header.transCode);                                 //交易码
        xmlWriter.writeElementClosed("cms_corp_no");                                                     //银企客户号
        xmlWriter.writeElementClosed("user_no");                                                         //用户号
        xmlWriter.writeElementClosed("org_code");                                                        //机构号
        xmlWriter.writeElementString("serial_no",header.serial_no,true);                                                       //交易流水号
        xmlWriter.writeElementString("req_no", header.reqSeqNo,true);
        if (header.reqDateTime == null) {
            header.reqDateTime = new Date();
        }
        xmlWriter.writeElementString("tr_acdt", Format.Date8Format.format(header.reqDateTime));  //交易日期
        xmlWriter.writeElementString("tr_time", Format.Time6Format.format(header.reqDateTime));  //交易时间
        xmlWriter.writeElementString("channel", "5");                                          //渠道标识
        xmlWriter.writeElementString("sign", "0");                                              //签名标识
        if(StringHelper.isNullOrEmpty(header.fileFlag)) {
            xmlWriter.writeElementString("file_flag", "0");                                        //文件标识
        }
        else{
            xmlWriter.writeElementString("file_flag", "1");
        }
        xmlWriter.writeElementClosed("reserved");                                                        //保留字段
        xmlWriter.writeEndElement();
    }
}
